//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Windows;
using System.Windows.Media;

namespace OpenLS.Spreadsheet.UI
{
    class PaginationResult
    {
        public int PageCount { get; set; }
    }
    class WorksheetPage : IWorksheetFragment
    {
        private readonly MultiCellsRange _range;
        private readonly int _number;
        private readonly PaginationResult _paginationResult;
        public IEnumerable<Row> Rows { get { return _range.GetRows(); } }
        public IEnumerable<Column> Columns { get { return _range.GetColumns(); } }
        public WorksheetPage(MultiCellsRange range, int number, PaginationResult paginationResult)
        {
            Contract.Requires(range != null);
            Contract.Requires(number > 0);
            _range = range;
            _number = number;
            _paginationResult = paginationResult;
        }

        public ActualHeaderFooter ActualHeader
        {
            get
            {
                var hf = this.Worksheet.HeaderFooter;
                if (hf == null)
                    return null;
                FontFamily fontFamily = new FontFamily("Arial");
                if (this.Worksheet.Workbook.Theme != null)
                    fontFamily = this.Worksheet.Workbook.Theme.FontScheme.MinorFont.Latin;
                if (Number == 1 && hf.DifferentFirst)
                    return ActualHeaderFooter.Parse(hf.FirstHeader, Fn, fontFamily);
                if (Number % 2 == 1)
                    return ActualHeaderFooter.Parse(hf.OddHeader, Fn, fontFamily);
                return ActualHeaderFooter.Parse(hf.EvenHeader, Fn, fontFamily);
            }
        }

        private string Fn(char arg)
        {
            switch (arg)
            {
                case 'P':
                    return this.Number.ToString();
                case 'N':
                    return this._paginationResult.PageCount.ToString();
                    ;
                case 'D':
                    return DateTime.Today.ToShortDateString();
                case 'T':
                    return DateTime.Now.ToString("T");
                case 'Z':
                    return Path.GetDirectoryName(Worksheet.Workbook.Document.FileName) + Path.DirectorySeparatorChar;
                case 'F':
                    return Path.GetFileName(Worksheet.Workbook.Document.FileName);
                case 'A':
                    return this.Worksheet.Name;
                default:
                    // Contract.Assert(false);
                    return arg.ToString();
            }
        }


        public ActualHeaderFooter ActualFooter
        {
            get
            {
                var hf = this.Worksheet.HeaderFooter;
                if (hf == null)
                    return null;
                FontFamily fontFamily = new FontFamily("Arial");
                if (this.Worksheet.Workbook.Theme != null)
                    fontFamily = this.Worksheet.Workbook.Theme.FontScheme.MinorFont.Latin;

                if (hf.DifferentFirst && Number == 1)
                    return ActualHeaderFooter.Parse(hf.FirstFooter, Fn, fontFamily);
                if (Number % 2 == 1)
                    return ActualHeaderFooter.Parse(hf.OddFooter, Fn, fontFamily);
                return ActualHeaderFooter.Parse(hf.EvenFooter, Fn, fontFamily);
            }
        }
        public Worksheet Worksheet
        {
            get { return _range.Worksheet; }
        }
        public void Resize(Size size)
        {

        }

        public double? DesiredWidth
        {
            get { return Enumerable.Sum(_range.GetColumns(), c => c.ActualWidth); }
        }

        public double? DesiredHeight
        {
            get { return Enumerable.Sum(_range.GetRows(), c => c.ActualHeight); }
        }

        public RangeAddress RangeAddress
        {
            get { return _range.Address; }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IWorksheetFragment Members


        public IEnumerable<Cell> GetCells()
        {
            foreach (var cell in _range.GetNonEmptyCells())
            {
                yield return cell;
            }
        }

        #endregion

        #region IWorksheetFragment Members

        public bool Contains(Cell cell)
        {
            return _range.Contains(cell);
        }

        public IEnumerable<Column> GetColumns()
        {
            return _range.GetColumns();
        }

        public IEnumerable<Row> GetRows()
        {
            return _range.GetRows();
        }

        #endregion

        #region IWorksheetFragment Members

        public int Left { get { return _range.TopLeftCell.Column.Index; } set { } }
        public int Top { get { return _range.TopLeftCell.Row.Index; } set { } }
        public int Number
        {
            get { return _number; }
        }

        #endregion
    }
}